package com.sykj.servies.impl;

import com.sykj.dao.CustomerDao;
import com.sykj.dao.ProductDao;
import com.sykj.dao.ReviewDao;
import com.sykj.pojo.Customer;
import com.sykj.pojo.Product;
import com.sykj.pojo.Review;
import com.sykj.pojo.ReviewExample;
import com.sykj.servies.ReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class ReviewServiceImpl implements ReviewService {
    @Autowired
    ReviewDao reviewDao;
    @Autowired
    CustomerDao customerDao;
    @Autowired
    ProductDao productDao;
    @Override
    public List<Review> getReviewListByPid(int id) {
        ReviewExample example = new ReviewExample();
        example.createCriteria().andPidEqualTo(id);
        List<Review> reviews = reviewDao.selectByExample(example);
        for (Review review:reviews){
            //得到商家名称
            Customer customer = customerDao.selectByPrimaryKey(review.getCstid());
            review.setCustomer(customer);
        }

        return reviews;
    }

    @Override
    public List<Review> list() {
        List<Review> reviews = reviewDao.selectByExample(null);
        for (Review review:reviews){
            Customer customer = customerDao.selectByPrimaryKey(review.getCstid());
            Product product = productDao.selectByPrimaryKey(review.getPid());
            review.setCustomer(customer);
            review.setProduct(product);
        }
        return reviews;
    }

    @Override
    public void save(Review entity) {
        reviewDao.insert(entity);
    }

    @Override
    public void del(int id) {
        reviewDao.deleteByPrimaryKey(id);
    }

    @Override
    public Review get(int id) {
        return reviewDao.selectByPrimaryKey(id);
    }
}
